package com.myrslok.install;

import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.net.URL;

/**
 * Represents a set of build scripts for a given OS.
 */
enum InstallScriptSet {
    /**
     * Generic windows fileset. Should work on 95,98,xp,vista,7
     */
    WIN("m.bat"),
    /**
     * Posix fileset. Uses standard 'sh' commands. No bash-specifics.
     * Tested for OS X and Linux.
     */
    POSIX("m.sh");
    private String scriptFileName;

    private InstallScriptSet(@NotNull final String scriptFileName) {
        this.scriptFileName = scriptFileName;
    }

    public String getScriptFileName() {
        return scriptFileName;
    }

    @NotNull
    public URL getResourceDirUrl() {
        final String resourceName = name().toLowerCase();
        final URL resource = getClass().getResource(resourceName);
        if (resource == null) {
            throw new IllegalStateException(String.format("Could not fetch the install script set directory as a resource. Install script set '%s'. Resource name '%s'.", name(), resourceName));
        }
        return resource;
    }

    public File getResourceDir() {
        return new File(getResourceDirUrl().getFile());
    }

    public static InstallScriptSet forOsName(@NotNull final String osName) {
        // Java 7 (incomplete openjdk port) on mac not worth it
        if ("Windows 7".equals(osName))
            return WIN;
        else if ("Mac OS X".equals(osName))
            return POSIX;
        else if ("Linux".equals(osName))
            return POSIX;
        else
            throw new IllegalArgumentException("Unsupported os name: " + osName);
    }

    @NotNull
    public static String getCurrentOsName() {
        return System.getProperty("os.name");
    }

    public static InstallScriptSet forCurrentOsName() {
        return forOsName(getCurrentOsName());
    }
}
